package com.augurit.common.syscode.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.dao.SysCodeItemDao;
import com.augurit.common.dao.SysCodeTypeDao;
import com.augurit.common.entity.SysCodeItem;
import com.augurit.common.entity.SysCodeType;
import com.augurit.common.syscode.convert.SysCodeConverter;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;

/**
 * 业务字典逻辑实现类
 */
@Service
@Transactional
public class SysCodeServiceImpl implements ISysCodeService {
	
	// DAO 引用
	@Autowired
	private SysCodeTypeDao sysCodeTypeDao; //业务字典类型DAO类
	
	@Autowired
	private SysCodeItemDao sysCodeItemDao; //业务字典项DAO类


	/**
	 * 根据ID获取业务字典项Form对象
	 * @param id 业务字典项主键ID
	 * @return 业务字典项Form对象
	 */
	@Transactional(readOnly = true)
	public SysCodeForm getItem(Long itemId){
		if(itemId != null){
			SysCodeItem entity = sysCodeItemDao.get(itemId);
			return SysCodeConverter.convertToForm(entity);
		}else
			return null;
	}
	
	/**
	 * 根据ID获取业务字典类型Form对象
	 * @param typeId 业务字典类型主键ID
	 * @return 业务字典类型Form对象
	 */
	@Transactional(readOnly = true)
	public SysCodeForm getType(Long typeId){
		if(typeId != null){
			SysCodeType entity = sysCodeTypeDao.get(typeId);
			return SysCodeConverter.convertToForm(entity);
		}else
			return null;
	}
	
	/**
	 * 根据业务字典类型编号获取业务字典类型Form对象
	 * @param itemCode 业务字典类型编号
	 * @return 业务字典类型Form对象
	 */
	@Transactional(readOnly = true)
	public SysCodeForm getType(String typeCode){
		if(typeCode != null && typeCode.trim().length() > 0){
			SysCodeType entity = sysCodeTypeDao.getByTypeCode(typeCode);
			return SysCodeConverter.convertToForm(entity);
		}else
			return null;
	}
	
	@Transactional(readOnly = true)
	public List<SysCodeForm> getItems(String typeCode){
		List<SysCodeItem> items = sysCodeItemDao.getByTypeCode(typeCode);
		if(items != null && items.size() > 0)		
			return SysCodeConverter.convertItemToFormList(items);
		else
			return null;
	}
	
	@Transactional(readOnly = true)
	public List<SysCodeForm> getItems(String typeCode, Map<String, List<SysCodeForm>> sysCodeBuffer){
		List<SysCodeForm> items = null;
		
		if(sysCodeBuffer != null && sysCodeBuffer.size() > 0){
			items = sysCodeBuffer.get(typeCode);
		}
		
		if(items == null || items.size() == 0){
			items = this.getItems(typeCode);
		}
			
		return items;
	}
	
	/**
	 * 根据分页对象和Form对象进行分页条件查询
	 * @param page 分页对象
	 * @param form Form对象
	 * @return 分页查询结果.附带结果列表及所有查询时的参数
	 */
	@Transactional(readOnly = true)
	public Page<SysCodeForm> searchItem(Page<SysCodeForm> page, SysCodeForm form) {
		
		// 建立新的分页对象
		Page<SysCodeItem> pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from SysCodeItem ps where 1=1 ");
		List values = new ArrayList();
		
		if(form != null){
			if(StringUtils.isNotBlank(form.getTypeCode())){
				hql.append(" and ps.typeCode = ?");
				values.add(form.getTypeCode());
			}else{
				hql.append(" and 1!=1");
			}
			if(StringUtils.isNotBlank(form.getItemCode())){
				hql.append(" and ps.code like ?");
				values.add("%" + form.getItemCode() + "%");
			}
			if(StringUtils.isNotBlank(form.getItemName())){
				hql.append(" and ps.name like ?");
				values.add("%" + form.getItemName() + "%");
			}
		}
		
		//排序
		pg.setOrderBy(SysCodeForm.convertToItemPropertyName(page.getOrderBy()));
		hql.append(HqlUtils.buildOrderBy(pg, "ps"));
		
		// 执行分页查询操作
		pg = sysCodeItemDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysCodeForm> list = SysCodeConverter.convertItemToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据分页对象和Form对象进行分页条件查询
	 * @param page 分页对象
	 * @param form Form对象
	 * @return 分页查询结果.附带结果列表及所有查询时的参数
	 */
	@Transactional(readOnly = true)
	public Page<SysCodeForm> searchType(Page<SysCodeForm> page, SysCodeForm form) {
		
		// 建立新的分页对象
		Page<SysCodeType> pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from SysCodeType ps where 1=1 ");
		List values = new ArrayList();
		if(form != null){
			
			if(StringUtils.isNotBlank(form.getTypeCode())){
				hql.append(" and ps.code like ?");
				values.add("%" + form.getTypeCode() + "%");
			}
			if(StringUtils.isNotBlank(form.getTypeName())){
				hql.append(" and ps.name like ?");
				values.add("%" + form.getTypeName() + "%");
			}
			if(StringUtils.isNotBlank(form.getTypeGrade())){
				hql.append(" and ps.grade = ?");
				values.add(form.getTypeGrade());
			}
			if(StringUtils.isNotBlank(form.getTypeMemo())){
				hql.append(" and ps.memo like ?");
				values.add("%" + form.getTypeMemo() + "%");
			}
		}
		
		//排序
		pg.setOrderBy(SysCodeForm.convertToTypePropertyName(page.getOrderBy()));
		hql.append(HqlUtils.buildOrderBy(pg, "ps"));	
		
		// 执行分页查询操作
		pg = sysCodeTypeDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysCodeForm> list = SysCodeConverter.convertTypeToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	/**
	 * 保存业务字典项Form对象数组
	 * @param forms Form对象数组
	 */
	public void saveItem(List<SysCodeForm> forms){
		if(forms != null && forms.size() > 0)
			for(SysCodeForm form : forms)
				sysCodeItemDao.save(form);
	}

	/**
	 * 保存业务字典类型Form对象数组
	 * @param forms Form对象数组
	 */
	public void saveType(List<SysCodeForm> forms){
		if(forms != null && forms.size() > 0)
			for(SysCodeForm form : forms)
				sysCodeTypeDao.save(form);
	}

	/**
	 * 根据主键ID集合删除子项对象
	 * @param itemIds 子项主键ID集合
	 * @return 被删除的子项所属的类型编号
	 */
	public List<String> deleteItem(Long... itemIds) {
		return sysCodeItemDao.deleteByItemIds(itemIds);
	}
	
	/**
	 * 根据主键ID集合删除多个业务字典类型对象
	 * @param ids 业务字典类型主键ID集合
	 * @return 被删除的类型对象的类型编号列表
	 */
	public List<String> deleteType(Long... typeIds) {
		return sysCodeTypeDao.deleteByTypeIds(typeIds);
	}
	
	/**
	 * 删除业务字典类型及所有业务字典项
	 * @param typeCode 业务字典类型编号
	 */
	public void deleteByTypeCode(String typeCode){
		sysCodeTypeDao.deleteByTypeCode(typeCode);
	}
	
	/**
	 * 得到所有数据字典
	 * @return
	 * cuigeqiang
	 */
	public List<SysCodeType> getAllCodeType(){
		return sysCodeTypeDao.getAll();
	}
}
